/**
 * Created by Ession on 2016/1/11.
 */
var test = {
    init: function () {
        var para =$.parseJSON(window.localStorage.getItem("para"));
        var pa = [];
        if(!Util.isEmpty(para)){
            $.each(para.para,function(i,n){
                pa.push(n.fieldName)
            });
        }
        var url = window.localStorage.getItem("testurl"),
            key = window.localStorage.getItem("testkey"),
            type = window.localStorage.getItem("testtype");
        if(!Util.isEmpty(url,key,type)){
            $("#url").val(url);
            $("#key").val(key);
            $("#test-type").find("option[value='post']").attr("selected","selected");
        }
        var temp = Util.template($("#para-template").html(),{data:pa,key:key});

        $(".test-content-param").html(temp);

        $("body").delegate(".fa-minus-circle","click",function(){
            $(this).parent().remove();
        });

        $("#btn-add-param").click(function () {
            var html = $(Util.template($("#add-para-template").html(),{data:pa}));
            $(".test-content-param").append(html);
        });

        $(window).unload(function(){
            localStorage.removeItem("testurl");
            localStorage.removeItem("testkey");
            localStorage.removeItem("testtype");
            localStorage.removeItem("para");
        });
    },
    test:function (){
        var url = $("#url").val();
        var type = $("#test-type").val();
        if(Util.isEmpty(url)){
            alert("请将参数填写完整");
            return false;
        }else{
            var param = {},parameters = "";
            param["url"] = url;
            $(".test-param-row").each(function(){
                var $firstVal = $(this).find(".test-param-cell:nth-child(1)");

                var key = Util.isEmpty($firstVal.find("input").val())?$firstVal.find("select").val():$firstVal.find("input").val();
                console.log(key)
                var value = $(this).find(".test-param-cell:nth-child(2)").find("input").val();
                if(Util.isEmpty(key)){
                    return true;
                }else{
                    parameters += Util.isEmpty(parameters)?"{\""+key+"\":\""+value+"\"":",\""+key+"\":\""+value+"\"";
                }
            });
            parameters += "}";
            param["parameters"] = parameters;
            //param["from"] = $(".test-param-row.from>.test-param-cell:nth-child(2)>input").val();
            //param["size"] = $(".test-param-row.size>.test-param-cell:nth-child(2)>input").val();
            //param["key"] = $(".test-param-row.key>.test-param-cell:nth-child(2)>input").val();
            $.ajax({
                url:"http://" + window.location.host+"/rmp/services/privateapis/esb/transfer",
                type:"post",
                dataType: "json",
                data:param,
                success: function (data) {
                    if(!Util.isEmpty(data.count)){
                        var columns = [],_data = data.datas[0],headers = [];
                        for(var k in _data){
                            columns.push(k);
                        }
                        for(var i=0;i<columns.length;i++){
                            var header = {};
                            header["field"] = columns[i];
                            header["name"] = columns[i];
                            headers.push(header);
                        }
                        $(".test-result").cakeTable({
                            head: headers,
                            showNum:false,
                            sort: false,
                            showColumns:false,
                            showPage:false,
                            columns: data.datas,
                            align:"center",
                            fixedHeader:false
                        });
                    }else{
                        alert("无数据！");
                        $(".test-result").empty();
                    }
                },
                error: function (data) {
                    alert("测试失败！");
                    $(".test-result").empty();
                }
            })
        }
    },
    empty: function () {
        $(".test-param-row").each(function () {
            if(!$(this).hasClass("key")){
                $(this).remove();
            }else{
                $(this).find(".test-param-cell:nth-child(2)").find("input").val("");
            }
        });
        $("#url").val("");
        $(".test-result").empty()
    }
};
